<?php
class M_DbTable_GroupAction extends Apps_Db_Table_Abstract {
    protected $_name = 'user_group_acl_action';

    public function getGroupActionDetail($groupId) {
		$select = $this->getAdapter()
					   ->select()
					   ->from('user_group_acl_action', null)
					   ->join('acl_action', 'acl_action.id 	= user_group_acl_action.acl_action_id', null)
					   ->join('acl_controller', 'acl_controller.id 	= acl_action.acl_controller_id', null)
					   ->join('acl_module', 	'acl_module.id 		= acl_controller.acl_module_id', null)
					   ->where('user_group_acl_action.user_group_id = ?', $groupId)
					   ->columns(array(
					       'module' 	=> 'acl_module.name',
					   	   'controller' => 'acl_controller.name',
					   	   'action' 	=> 'acl_action.name'
					   ));
		$result = $select->query()->fetchAll();
		return $result;
    }

    public function getResources() {
    	$select = $this->getAdapter()
					   ->select()
					   ->from('acl_controller', null)
					   ->joinRight('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
					   ->columns(array(
					       'module' 	=> 'acl_module.name',
					   	   'controller' => 'acl_controller.name'
					   ));
		$result = $select->query()->fetchAll();
		return $result;
    }

    // array action by module name and controller name action name
    public function getAction($moduleName, $controllerName,$actionName) {
    	$select = $this->getAdapter()
    				   ->select()
    				   ->from('acl_action', null)
					   ->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
					   ->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
					   ->where('acl_module.name = ?', $moduleName)
					   ->where('acl_controller.name = ?', $controllerName)
					   ->where('acl_action.name = ?', $actionName)
					   ->columns(array(
					       'id' 	=> 'acl_action.id'
					   ));
		$result = $select->query()->fetch();
		return $result;
    }

    // array action by module name and controller name
    public function getArrayAction($moduleName, $controllerName) {
    	$select = $this->getAdapter()
    				   ->select()
    				   ->from('acl_action', null)
					   ->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
					   ->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
					   ->where('acl_module.name = ?', $moduleName)
					   ->where('acl_controller.name = ?', $controllerName)
					   ->columns(array(
					       'id' 	=> 'acl_action.id'
					   ));
		$result = $select->query()->fetchAll();
		return $result;
    }

    // Array Action by Module name
	public function getActionByModule($moduleName) {
    	$select = $this->getAdapter()
    				   ->select()
    				   ->from('acl_action', null)
					   ->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
					   ->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
					   ->where('acl_module.name = ?', $moduleName)
					   ->columns(array(
					       'id' 	=> 'acl_action.id'
					   ));
		$result = $select->query()->fetchAll();
		return $result;
    }

    public function getActionIndexOrList($moduleName) {
    	$select = $this->getAdapter()
    				   ->select()
    				   ->from('acl_action', null)
					   ->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
					   ->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
					   ->where('acl_module.name = ?', $moduleName)
					   ->where('acl_action.name = ?', 'index')
					   ->orWhere('acl_action.name = ?', 'list')
					   ->columns(array(
					       'id' 	=> 'acl_action.id'
					   ));
		$result = $select->query()->fetch();
		return $result;
    }


    public function getListActionByModule($moduleName) {
    	$select = $this->getAdapter()
			    	->select()
			    	->from('acl_action', null)
			    	->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
			    	->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
			    	->where('acl_module.name = ?', $moduleName)
			    	->columns(array(
			    			'controller_name' => 'acl_controller.name',
			    			'action_name' 	=> 'acl_action.name',
			    			'controller_id'	=> 'acl_controller.id',
			    			'id' => 'acl_action.id'
			    	));
    	$result = $select->query()->fetchAll();
    	return $result;
    }

    public function getListActionFromGroupAction($userGroupId) {
		$select = $this->getAdapter()
				    	->select()
				    	->from('user_group_acl_action', null)
				    	->join('acl_action', 'acl_action.id = user_group_acl_action.acl_action_id', null)
				    	->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
				    	->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
				    	->where('user_group_acl_action.user_group_id = ?', $userGroupId)
				    	->columns(array(
				    			'module_name'	  => 'acl_module.name',
				    			'controller_name' => 'acl_controller.name',
				    			'action_name' 	  => 'acl_action.name'))
				    	->distinct();
    	$result = $select->query()->fetchAll();
    	return $result;
    }

    public function getListActionManagement($userGroupId) {
    	$select = $this->getAdapter()
				    	->select()
				    	->from('user_group_acl_action', null)
				    	->join('acl_action', 'acl_action.id = user_group_acl_action.acl_action_id', null)
				    	->join('acl_controller', 'acl_controller.id = acl_action.acl_controller_id', null)
				    	->join('acl_module', 'acl_module.id = acl_controller.acl_module_id', null)
				    	->where('user_group_acl_action.user_group_id = ?', $userGroupId)
				    	->where('acl_module.name = ?', 'management')
				    	->columns(array(
				    			'action_name'	  => 'acl_action.name'))
				    	->distinct();
    	$result = $select->query()->fetchAll();
    	return $result;
    }
}